package com.devunion.salon.client.common;

import com.devunion.salon.client.app.bean.ProductModel;
import com.devunion.salon.client.app.bean.search.ProductSearchPagingLoadConfig;
import com.devunion.salon.client.rpc.ProductService;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.PagingToolBar;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spetrakovsky
 */
public class ProductList extends LayoutContainer {

    private Grid<ProductModel> productGrid;

    private BasePagingLoader<ProductSearchPagingLoadConfig, PagingLoadResult<ProductModel>> dataLoader;

    private PagingToolBar toolBar;

    private ProductSearchPagingLoadConfig loadConfig;

    private Field barCodeField;

    private Field nameField;

    private Field vendorField;

    private Button refreshButton;

    private LayoutContainer searchPanel;

    public ProductList() {
        super(new RowLayout());
    }

    public void initialize() {
        removeAll();
        initDataLoader();
        initToolBar();
        initGridPanel();
        initSearchPanel();
        intiListenets();
        add(searchPanel);
        add(productGrid);
        add(toolBar);
        setAutoHeight(true);

    }

    private void initDataLoader() {
        RpcProxy proxy = new RpcProxy<ProductSearchPagingLoadConfig, PagingLoadResult<ProductModel>>() {
            protected void load(ProductSearchPagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<ProductModel>> pagingLoadResultAsyncCallback) {
                ProductService.App.getInstance().findProducts(loadConfig, pagingLoadResultAsyncCallback);
            }
        };

        // dataLoader
        dataLoader = new BasePagingLoader<ProductSearchPagingLoadConfig, PagingLoadResult<ProductModel>>(proxy);
        loadConfig = new ProductSearchPagingLoadConfig();
        dataLoader.useLoadConfig(loadConfig);
        dataLoader.setRemoteSort(true);
        dataLoader.load(0, 15);
    }

    private void initToolBar() {
        toolBar = new PagingToolBar(15);
        toolBar.bind(dataLoader);
    }

    private void initGridPanel() {

        final ListStore<ProductModel> store = new ListStore<ProductModel>(dataLoader);
        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
        columns.add(new ColumnConfig("name", "Name", 150));
        columns.add(new ColumnConfig("barCode", "Bar Code", 100));
        columns.add(new ColumnConfig("retailPrice", "Retail Price", 200));
        columns.add(new ColumnConfig("uom", "UOM", 100));

        ColumnModel cm = new ColumnModel(columns);

        productGrid = new Grid<ProductModel>(store, cm);
        productGrid.setLoadMask(true);
        productGrid.setBorders(true);
        productGrid.setAutoExpandColumn("name");
        productGrid.setAutoHeight(true);

        dataLoader.addListener(Loader.BeforeLoad, new Listener() {
            public void handleEvent(BaseEvent be) {
                loadConfig.setBarCode((String) barCodeField.getValue());
                loadConfig.setName((String) nameField.getValue());
                loadConfig.setVendor((String) vendorField.getValue());
            }
        });
    }

    private void initSearchPanel() {
        searchPanel = new HorizontalPanel();

        searchPanel.add(new Label("Name"));
        nameField = new TextField();
        searchPanel.add(nameField);

        searchPanel.add(new Label("Bar Code"));
        barCodeField = new TextField();
        searchPanel.add(barCodeField);

        searchPanel.add(new Label("Vendor name"));
        vendorField = new TextField();
        searchPanel.add(vendorField);

        refreshButton = new Button("Search");
    }

    private void intiListenets() {
        refreshButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
            public void componentSelected(ButtonEvent ce) {
                toolBar.refresh();
            }
        });
    }

    public Grid<ProductModel> getProductGrid() {
        return productGrid;
    }

    public void setProductGrid(Grid<ProductModel> productGrid) {
        this.productGrid = productGrid;
    }

    public BasePagingLoader getDataLoader() {
        return dataLoader;
    }

    public void setDataLoader(BasePagingLoader dataLoader) {
        this.dataLoader = dataLoader;
    }

    public PagingToolBar getToolBar() {
        return toolBar;
    }

    public void setToolBar(PagingToolBar toolBar) {
        this.toolBar = toolBar;
    }

    public ProductSearchPagingLoadConfig getLoadConfig() {
        return loadConfig;
    }

    public void setLoadConfig(ProductSearchPagingLoadConfig loadConfig) {
        this.loadConfig = loadConfig;
    }

    public Field getBarCodeField() {
        return barCodeField;
    }

    public void setBarCodeField(Field barCodeField) {
        this.barCodeField = barCodeField;
    }

    public Field getNameField() {
        return nameField;
    }

    public void setNameField(Field nameField) {
        this.nameField = nameField;
    }

    public Field getVendorField() {
        return vendorField;
    }

    public void setVendorField(Field vendorField) {
        this.vendorField = vendorField;
    }

    public Button getRefreshButton() {
        return refreshButton;
    }

    public void setRefreshButton(Button refreshButton) {
        this.refreshButton = refreshButton;
    }

    public LayoutContainer getSearchPanel() {
        return searchPanel;
    }

    public void setSearchPanel(LayoutContainer searchPanel) {
        this.searchPanel = searchPanel;
    }
}
